package com.zhuiyun.project.security.commonhandler;

import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.common.response.ResponseUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName CustomizeAuthenticationFailureHandler
 * @Description 登录失败处理逻辑
 * @Author zcy
 * @Date 2023/3/27 11:48
 **/
@Component
public class UserLoginAuthenticationFailureHandler implements AuthenticationFailureHandler {
    /**
     *
     * 封装失败原因并返回
     * @param request
     * @param response
     * @param e
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        // 封装异常信息
        CommonResult commonResult = CommonResult.error(e.getMessage());
        ResponseUtil.responseJson(response,commonResult);
        }
}
